//
//  YLEditAdressViewController.m
//  YLFinanceService
//
//  Created by she1323 on 2017/9/8.
//  Copyright © 2017年 she1323. All rights reserved.
//  新增/修改地址

#import "YLEditAdressViewController.h"
#import "ZmjPickView.h"
#import "YLEditAdressViewModel.h"
#import "UIImage+YFResizing.h"
#import "YLAdressManagerViewController.h"
#import "YLMyAdressModel.h"
#import "JSAddressPickerView.h"
#import "YLGlobeHeader.h"
@interface YLEditAdressViewController ()<JSAddressPickerDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (strong, nonatomic) ZmjPickView *zmjPickView;
// 详细地址
@property (weak, nonatomic) IBOutlet UIButton *adressButton;
// 名字
@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
// 手机
@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
// 详细地址
@property (weak, nonatomic) IBOutlet UITextField *detailAdressTextField;

@property (nonatomic, strong) YLEditAdressViewModel *viewModel;
@property (weak, nonatomic) IBOutlet UIButton *finshButton;

@property (nonatomic, strong) JSAddressPickerView *pickerView;
@end

@implementation YLEditAdressViewController

- (YLEditAdressViewModel *)viewModel
{
    if (!_viewModel) {
        _viewModel = [[YLEditAdressViewModel alloc] init];
    }
    return _viewModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if (_adressModel) {
        _nameTextField.text = _adressModel.receiveName;
        _phoneTextField.text = _adressModel.receiveTel;
        [_adressButton setTitle:[NSString stringWithFormat:@"%@ %@ %@", _adressModel.province, _adressModel.city, _adressModel.area] forState:UIControlStateNormal];
        _detailAdressTextField.text = _adressModel.receiveAddress;
        
        self.viewModel.adressModel = self.adressModel;
        self.title = @"修改地址";
    } else {
        self.title = @"添加地址";
    }
    
    
    _scrollView.alwaysBounceVertical = YES;
    [_finshButton setBackgroundImage:[UIImage yf_imageWithColor:[UIColor lightGrayColor]] forState:UIControlStateDisabled];
     [_finshButton setBackgroundImage:[UIImage yf_imageWithColor:YLLOrgColor] forState:UIControlStateNormal];
    
    [self bindViewModel];
}

#pragma mark 创建地址选择器
- (void)enterAddress {
    if (self.pickerView) {
        self.pickerView.hidden = NO;
        return;
    }
    self.pickerView = [[JSAddressPickerView alloc] initWithFrame:CGRectMake(0, 0, YLScreenW, YLScreenH)];
    //[self.pickerView updateAddressAtProvince:@"上海" city:@"厦门市" town:@"集美区"];
    self.pickerView.delegate = self;
    self.pickerView.font = [UIFont boldSystemFontOfSize:14];
    [self.view addSubview:self.pickerView];
}

- (void)JSAddressCancleAction:(id)senter {
    self.pickerView.hidden = YES;
}

#pragma mark 选择地址
- (void)JSAddressPickerRerurnBlockWithProvince:(NSString *)province city:(NSString *)city town:(NSString *)town {
    self.pickerView.hidden = YES;
//    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:3];
//    dict[@"province"] = ;
//    dict[@"city"] = ;
//    dict[@"district"] = ;
//   // _scrollView.adressDict = dict;
    
    
    [self.adressButton setTitle:[NSString stringWithFormat:@"%@ %@ %@", province, city, town] forState:UIControlStateNormal];
    self.viewModel.province = province;
    self.viewModel.city = city;
    self.viewModel.area = town;
}




- (void)bindViewModel
{
    RAC(self.viewModel, nameText) = _nameTextField.rac_textSignal;
    RAC(self.viewModel, adressText) = RACObserve(self.adressButton.titleLabel, text);
    RAC(self.viewModel, detailAdressText) = _detailAdressTextField.rac_textSignal;
    RAC(self.viewModel, phoneText) = _phoneTextField.rac_textSignal;
    
    RAC(_finshButton, enabled) = self.viewModel.finshEnableSingal;
}

- (ZmjPickView *)zmjPickView {
    if (!_zmjPickView) {
        _zmjPickView = [[ZmjPickView alloc]init];
    }
    return _zmjPickView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)editAdress:(UIButton *)sender {
    
    [self.view endEditing:YES];
    [self enterAddress];
//    return;
//    [self zmjPickView];
//
//    [_zmjPickView show];
//
//    __weak typeof(self) weakSelf = self;
//
//    _zmjPickView.determineBtnBlock = ^(NSInteger shengId, NSInteger shiId, NSInteger xianId, NSString *shengName, NSString *shiName, NSString *xianName){
//    [weakSelf.adressButton setTitle:[NSString stringWithFormat:@"%@ %@ %@", shengName, shiName, xianName] forState:UIControlStateNormal];
//        weakSelf.viewModel.province = shengName;
//        weakSelf.viewModel.city = shiName;
//        weakSelf.viewModel.area = xianName;
//    };
}

- (IBAction)finshChange:(UIButton *)sender {
    
    __weak typeof(self) weakSelf = self;
    
    // 有传过来的地址模型 修改地址
    if (_adressModel) {
        [[self.viewModel.editAdressCommand execute:nil] subscribeNext:^(id  _Nullable x) {
            
            
            [weakSelf.navigationController popToViewController:self.navigationController.childViewControllers[self.navigationController.childViewControllers.count - 4] animated:YES];
        }];
        
    } else {
        // 添加地址
        [[self.viewModel.addAdressCommand execute:nil] subscribeNext:^(id  _Nullable x) {
            
            if (self.addAdressBlock) {
                self.addAdressBlock(weakSelf.viewModel.city);
            }
            
            if (self.delegate && [self.delegate respondsToSelector:@selector(edidAddressViewController:addAddress:)]) {
                                [self.delegate edidAddressViewController:self addAddress:x];
            }
            
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }];
    }
    
    
}


@end
